import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login'
import layout from '../views/layout'
import home from '../views/layout/home'
import question from '../views/layout/question'
import movie from '../views/layout/movie'
import user from '../views/layout/user'
import edit from '../views/layout/edit'
import article from '../views/layout/article'
import search from '../views/layout/search'
import results from '../views/layout/results'
import student from '../views/layout/student'
import { getToken, removeToken } from '../utils/token'
import { Toast } from 'vant'
import { getUserInfo } from '../api/user'
import store from '../store'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  // 路由的重定向
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/',
    name: 'layout',
    component: layout,
    children: [
      {
        path: 'home',
        name: 'home',
        component: home
      },
      {
        path: 'question',
        name: 'question',
        component: question
      },
      {
        path: 'movie',
        name: 'movie',
        component: movie
      },
      {
        path: 'user',
        name: 'user',
        component: user,
        meta: {
          needLogin: true
        }
      },
      {
        path: 'edit',
        name: 'edit',
        component: edit,
        meta: {
          needLogin: true,
          needHide: true
        }
      },
      {
        path: 'article',
        name: 'article',
        component: article
      },
      {
        path: 'search',
        name: 'search',
        component: search
      },
      {
        path: 'results',
        name: 'results',
        component: results
      },
      {
        path: 'student', // /student
        name: 'student',
        component: student,
        meta: {
          // 标记需要登录
          needLogin: true
        }
      }
    ]
  }
]
// 吞掉 没有处理的 promise的错误
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) {
    // 执行之后 阻断后续代码
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
  routes
})
// 添加导航守卫 全局前置守卫 切换之前执行
router.beforeEach((to, from, next) => {
  // console.log('to:', to)
  // 通过meta判断是否需要检查登录
  // if (to.meta.needLogin !== true) {
  //   // 可以直接访问
  //   return next()
  // }
  // vuex中有用户信息 不需要做后续判断,肯定可以访问
  if (store.state.userInfo.name !== undefined) {
    // 本地有用户信息
    return next()
  }

  // 获取token
  const tokenObj = getToken()
  // token为空 但是这个页面不需要登录 放行
  if (tokenObj === null && to.meta.needLogin !== true) {
    // 放行
    return next()
  }
  // // token为空 但是这个页面需要登录 去登陆
  if (tokenObj === null && to.meta.needLogin === true) {
    // token为空
    Toast.fail('请先登录')
    // 去登录
    return next({ path: '/login', query: { redirect: to.path } })
  }

  // token不为空 判断对错
  getUserInfo()
    .then(res => {
      store.commit('setuserInfo', res.data.data)
      next()
    })
    .catch(errRes => {
      // token有错误
      // if (errRes.response.status === 401 || errRes.response.status === 400) {
      if (errRes.response.status === 401) {
        // 页面需要登录 去登陆
        // token有问题
        // 删掉
        removeToken()
        if (to.meta.needLogin === true) {
          // 提示
          Toast.fail('请先登录')
          // 去登录
          return next({ path: '/login', query: { redirect: to.path } })
        }
        // 页面不需要登录 放心
        return next()
      }
    })
})

export default router
